package com.hkj.genealogy.genealogydemo.controller;

import com.hkj.genealogy.genealogydemo.entity.Member;
import com.hkj.genealogy.genealogydemo.entity.User;
import com.hkj.genealogy.genealogydemo.service.MemberServiceImp;
import com.hkj.genealogy.genealogydemo.service.UserServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

/**
 * @program: genealogydemo
 * @description: 页面跳转控制类
 * @author: Hekejian
 * @time: 2020-07-04 23:01
 **/
@Controller
public class PageController {
    @Autowired
    MemberServiceImp memberServiceImp;
    @Autowired
    UserServiceImp userServiceImp;
    @RequestMapping("/")
    public String home(){
        return "login";
    }
    @RequestMapping("/register")
    public String registerPage(){
        return "register";
    }

    @RequestMapping("/success")
    public String success(){
        return "success";
    }

    @RequestMapping("/home")
    public String goHome(Map<String,Object> map, Authentication authentication){
        String name = authentication.getName();
        User userInfo = userServiceImp.getUserInfo(name);
        Long fid = userServiceImp.gerUserFid(userInfo.getId());
        List<Member> allMember = memberServiceImp.getAllMember(fid);
        System.out.println("进入首页");
        map.put("members",allMember);
        return "home";
    }
    @RequestMapping("/createRoot")
    public String createRoot(Map<String,Object> map,Authentication authentication){
        String name = authentication.getName();
        User userInfo = userServiceImp.getUserInfo(name);
        Long fid = userServiceImp.gerUserFid(userInfo.getId());
        map.put("fid",fid);
        return "createRoot";
    }
    @RequestMapping("/createChildren")
    public String createChildren(Map<String,Object> map, @RequestParam(value = "fid")Long fid, @RequestParam(value = "pid")Long pid){
        map.put("pid",pid);
        map.put("fid",fid);
        return "addChildren";
    }
    @RequestMapping("/getGenealogyPage")
    public String getGenealogyPage(){
        return "genealogy";
    }
    @RequestMapping("/createAccount")
    public String createAccount(Map<String,Object> map,@RequestParam(value = "id")Long id){
        map.put("id",id);
        return "createAccount";
    }
}
